package com.google.android.helan;

import com.google.android.gtalkservice.IGTalkService;
import com.google.android.gtalkservice.IGTalkSession;
import com.google.android.helan.R;
import com.google.android.helan.R.drawable;
import com.google.android.helan.R.string;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Client extends Activity {

	private static final String LOG_TAG = "Client";
	
	IGTalkSession gtsession=null;
	EditText uidField;
	EditText textField;
	Button sendButton;
	
	@Override
	protected void onCreate(Bundle icicle) {
		// TODO Auto-generated method stub
		super.onCreate(icicle);
		
		setContentView(R.layout.main);
		
		uidField=(EditText) findViewById(R.id.username);
		//uidField.setOnClickListener(onClickListener);
		//uidField.requestFocus();
		
		textField=(EditText) findViewById(R.id.message_text);
		
		sendButton=(Button) findViewById(R.id.send);
		//sendButton.setOnClickListener(onClickListener);
		sendButton.setEnabled(false);
		
		bindGTalk();
		
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		//unbindService(connection);
	}
	
	private void bindGTalk(){
		bindService((new Intent()).setComponent(com.google.android.
				gtalkservice.GTalkServiceConstants.GTALK_SERVICE_COMPONENT), 
				connection , 0);
	}
	
	private boolean isValidUsername(String username) {
        if (TextUtils.isEmpty(username)) {
            return false;
        }

        if (username.indexOf('@') == -1) {
            return false;
        }

        return true;
    }
	
	private void logMessage(CharSequence msg) {
        NotificationManager nm = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE);

        nm.notify(123,new Notification((Context)this,R.drawable.icon,msg,
        		System.currentTimeMillis(),(CharSequence)"",(CharSequence)"",
        		new Intent(Intent.DEFAULT_ACTION),R.drawable.icon,
        		(CharSequence)"Client",new Intent(Intent.DEFAULT_ACTION)));
    }
	
	private ServiceConnection connection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
			IGTalkService gtalkService= IGTalkService.Stub.asInterface(service);
			try{
				gtsession=gtalkService.getDefaultSession();
				if(gtsession==null){
					logMessage(getText(R.string.xmpp_session_not_found));
                    return;
				}
			}catch (DeadObjectException doe){
				 Log.e(LOG_TAG, "caught " + doe);
	             logMessage(getText(R.string.found_stale_xmpp_service));
			}
			sendButton.setEnabled(true);
		}
		 public void onServiceDisconnected(ComponentName className) {
			 gtsession=null;
			 sendButton.setEnabled(false);
		 }
	};
	
	private View.OnClickListener onClickListener = new View.OnClickListener(){
		 public void onClick(View v) {
			 Uri.Builder ub=new Uri.Builder();
			 ub.encodedFragment(textField.getText().toString());
			 Uri u=ub.build();
			 
			 if(v==uidField){
				 sendButton.requestFocus();
			 }
			 else{
				 String username = uidField.getText().toString();
	             if (!isValidUsername(username)) {
	                 logMessage(getText(R.string.invalid_username));
	                 return;
	             }

	             if (gtsession == null) {
	                 logMessage(getText(R.string.xmpp_service_not_connected));
	                 return;
	             }
	             try{
	            	 gtsession.sendDataMessage(username, new Intent().setData(u));
	             }catch (DeadObjectException doe){
					 Log.e(LOG_TAG, "caught " + doe);
		             logMessage(getText(R.string.found_stale_xmpp_service));
				}
	             
			 }
		 }
		 
	};
	

}
